<?php
/**
 *
 * 版权所有：安德玛(underArmour)
 * 作    者：1554324405@qq.com
 * 日    期：2020-05-20
 * 版    本：1.0.0
 * 功能说明：发送验证码控制器。
 *
 **/
namespace Api\Controller;

class SendSmsController extends BaseController
{
    public function _initialize(){
//         header('Content-Type:application/json; charset=utf-8');
//         header('Access-Control-Allow-Origin:*');
//         header('Access-Control-Allow-Methods:*');
//         header('Access-Control-Allow-Headers:*');
    }
    
    
    /**
     * 发送验证码
     * @param string $mobile
     * @param string $type：1注册，2忘记密码，3修改密码
     */
    public function index(){
        $mobile = I('get.mobile');
        $type = I('get.type');
        if(!$mobile){
            $this->res['code'] = 101;
            $this->res['msg'] = '手机号为空';
            $this->response($this->res,'json');
        }
        if(!$type){
            $this->res['code'] = 102;
            $this->res['msg'] = '验证码类型为空';
            $this->response($this->res,'json');
        }
        if(!$this->isphone($mobile)){
            $this->res['code'] = 103;
            $this->res['msg'] = '手机号不合法';
            $this->response($this->res,'json');
        } 
        $mobile_count = M('user')->where(array('username'=>$mobile))->count();
        if($type == 1){
            // 注册判断手机号是否存在
            if($mobile_count > 0){
                $this->res['code'] = 104;
                $this->res['msg'] = '手机号已存在';
                $this->response($this->res,'json');
            }
        }else{
            // 判断手机号是否不存在
            if($mobile_count == 0){
                $this->res['code'] = 104;
                $this->res['msg'] = '手机号不存在';
                $this->response($this->res,'json');
            }
        }
        
        
        $code = $this->getRandomCode(4);
        //验证码存储数据库，方便验证有效期
        $num = M('sms_code')->where(array('mobile'=>$mobile))->count();
        if($num > 0){
            // 判断手机号是否存在恶意点击情况
            $sms_info = M('sms_code')->where(array('mobile'=>$mobile))->find();
            if(intval($sms_info['expire_t']) > time()){
                $this->res['code'] = 103;
                $this->res['msg'] = '您上一次的验证码还在有效期内，请勿重复发送验证码！';
                $this->response($this->res,'json');
            }
            M('sms_code')->where(array('mobile'=>$mobile))->save(array('code'=>$code,'expire_t'=>time() + 300,'update_t'=>time()));
        }else{
            M('sms_code')->add(array('mobile'=>$mobile,'code'=>$code,'expire_t'=>time() + 300,'update_t'=>time()));
        }
        // 构造短信发送参数
        $data = array(
            'Account'=>'18112037190',
            'Pwd'=>'a29a5ee2b49f287369ff64a43',
            'Content'=>$code,
            'Mobile'=>$mobile,
            'TemplateId'=>131249,
            'SignId'=>266629
        );
        $r = $this->smsPost("http://api.feige.ee/SmsService/Template",$data);
        if($r){
            $this->res['msg'] = 'success';
            $this->response($this->res,'json');
        }else{
            $this->res['code'] = -1;
            $this->res['msg'] = 'send failed';
            $this->response($this->res,'json');
        }
    }
    
    
    //验证手机号是否正确
    private function isphone($phone){
        if (!is_numeric($phone)) {
            return false;
        }
        return preg_match("/^1[34578]{1}\d{9}$/", $phone) ? true : false;
    }
    
    
    //获取随机数字字符串
    private function getRandomCode($length=4) {
        $glbVerifySeed = "1234567890";
        
        $bgnIdx = 0;
        $endIdx = strlen($glbVerifySeed)-1;
        
        $code = "";
        for($i=0; $i<$length; $i++) {
            $curPos = rand($bgnIdx, $endIdx);
            $code .= substr($glbVerifySeed, $curPos, 1);
        }
        
        return $code;
    }
    
    
    /**
     * 短信发送通道
     */
    private function smsPost($url="http://api.feige.ee/SmsService/Template", $data, $proxy = null, $timeout = 20)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); //在HTTP请求中包含一个"User-Agent: "头的字符串。
        curl_setopt($curl, CURLOPT_HEADER, 0); //启用时会将头文件的信息作为数据流输出。
        curl_setopt($curl, CURLOPT_POST, true); //发送一个常规的Post请求
        curl_setopt($curl,  CURLOPT_POSTFIELDS, $data);//Post提交的数据包
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); //启用时会将服务器服务器返回的"Location: "放在header中递归的返回给服务器，使用CURLOPT_MAXREDIRS可以限定递归返回的数量。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); //文件流形式
        curl_setopt($curl, CURLOPT_TIMEOUT, $timeout); //设置cURL允许执行的最长秒数。
        $content = curl_exec($curl);
        curl_close($curl);
        unset($curl);
        return $content;
    }
    
}